/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package include.fxml;

import include.Constants;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.Insets;
import javafx.geometry.InsetsBuilder;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFieldBuilder;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.*;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 *
 * @author David
 */
public class DbEnter extends Stage {
  
  public final double HEIGHT = 110.0;
  private double WIDTH = 300.0;

  private TextField tfText;
  private Button btnOk;
  private Button btnCancel;
  
  private boolean accept = false;
  
  public DbEnter(String prompt) {
    initialize(prompt);
  }

  public DbEnter(String prompt, StageStyle style) {
    super(style);
    initialize(prompt);
  }
  
  private void initialize(String prompt) {
    
    setTitle(Constants.APPLICATION_NAME);
    initModality(Modality.APPLICATION_MODAL);
    
    tfText = TextFieldBuilder.create().promptText(prompt).build();
    btnOk = ButtonBuilder.create().text("Ok").disable(true).defaultButton(true).build();
    btnCancel = ButtonBuilder.create().text(SlyumFX.getInstance().rb.getString("cancel")).build();
    
    addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {

      @Override
      public void handle(KeyEvent event) {
        if (event.getCode().equals(KeyCode.ESCAPE))
        {
          close();
          event.consume();
        }
      }
    });
    
    tfText.setOnKeyPressed(new EventHandler<KeyEvent>() {

      @Override
      public void handle(KeyEvent event) {
        btnOk.setDisable(tfText.getText().isEmpty());
      }
    });
    
    btnOk.setOnAction(new EventHandler<ActionEvent>() {

      @Override
      public void handle(ActionEvent event) {
        accept = true;
        close();
      }
    });
    
    btnCancel.setOnAction(new EventHandler<ActionEvent>() {

      @Override
      public void handle(ActionEvent event) {
        close();
      }
    });
    
    HBox hb = HBoxBuilder.create().spacing(3.0).padding(InsetsBuilder.create().top(5.0).build()).alignment(Pos.CENTER_RIGHT).build();
    hb.getChildren().addAll(btnOk, btnCancel);
    
    VBox vBoxText = new VBox();
    vBoxText.getChildren().add(tfText);
    BorderPane bp = BorderPaneBuilder.create().padding(new Insets(10.0)).center(vBoxText).bottom(hb).build();
    
    Scene scene = new Scene(bp, WIDTH, HEIGHT);
    setMaxHeight(HEIGHT);
    setMinHeight(HEIGHT);
    setScene(scene);
    centerOnScreen();
  }
  
  public String getUserEntry() {
    return tfText.getText();
  }
  
  public boolean isAccepted(){
    return accept;
  }  
}
